/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.ap.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.ap.entity.PaymentApplication;
import com.jeeplus.modules.ap.mapper.PaymentApplicationMapper;
import com.jeeplus.modules.ap.entity.PaymentDetil;
import com.jeeplus.modules.ap.mapper.PaymentDetilMapper;

/**
 * 付款申请Service
 * @author 李超辉
 * @version 2018-12-13
 */
@Service
@Transactional(readOnly = true)
public class PaymentApplicationService extends CrudService<PaymentApplicationMapper, PaymentApplication> {

	@Autowired
	private PaymentDetilMapper paymentDetilMapper;
	
	public PaymentApplication get(String id) {
		PaymentApplication paymentApplication = super.get(id);
		paymentApplication.setPaymentDetilList(paymentDetilMapper.findList(new PaymentDetil(paymentApplication)));
		return paymentApplication;
	}
	
	public List<PaymentApplication> findList(PaymentApplication paymentApplication) {
		return super.findList(paymentApplication);
	}
	
	public Page<PaymentApplication> findPage(Page<PaymentApplication> page, PaymentApplication paymentApplication) {
		return super.findPage(page, paymentApplication);
	}
	
	@Transactional(readOnly = false)
	public void save(PaymentApplication paymentApplication) {
		super.save(paymentApplication);
		for (PaymentDetil paymentDetil : paymentApplication.getPaymentDetilList()){
			if (paymentDetil.getId() == null){
				continue;
			}
			if (PaymentDetil.DEL_FLAG_NORMAL.equals(paymentDetil.getDelFlag())){
				if (StringUtils.isBlank(paymentDetil.getId())){
					paymentDetil.setPyment(paymentApplication);
					paymentDetil.preInsert();
					paymentDetilMapper.insert(paymentDetil);
				}else{
					paymentDetil.preUpdate();
					paymentDetilMapper.update(paymentDetil);
				}
			}else{
				paymentDetilMapper.delete(paymentDetil);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(PaymentApplication paymentApplication) {
		super.delete(paymentApplication);
		paymentDetilMapper.delete(new PaymentDetil(paymentApplication));
	}
	
}